import { Commit } from 'vuex'

export default {
  namespaced: true,
  state: {
    smsToken: '',
    menuList: [],
    menuListAll: []
  },
  mutations: {
    SETSMSTOKEN: (state: any, data: any) => {
      state.smsToken = data
    },
    MENULIST: (state: any, data: any) => {
      state.menuList = data
    }
  },
  actions: {
    setSmsToken({ commit, state }: any, data: any) {
      sessionStorage.setItem('sms_token', data)
      commit('SETSMSTOKEN', data)
    },
    setMenuList({ commit, state }: any, data: any) {
      const ls = data.split(',')
      const list = state.menuListAll.filter((item: any) => ls.includes(item.path))
      localStorage.setItem('menu_list', JSON.stringify(list))
      commit('MENULIST', list)
    }
  },
  getters: {
    getSmsToken: (state: any) => {
      if (state.smsToken) {
        return state.smsToken
      } else {
        return sessionStorage.getItem('sms_token') || ''
      }
    },
    getMenuList: (state: any) => {
      if (state.menuList.length > 0) {
        return state.menuList
      } else {
        const localMenuList = localStorage.getItem('menu_list')
        if (localMenuList) {
          return JSON.parse(localMenuList)
        } else {
          return []
        }
      }
    }
  }
}
